﻿#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MapEditor;
using System.Collections.Generic;
#endregion

namespace PFAGame.Screens
{
    class TestTileEngineScreen : GameScreen
    {
        #region Fields

        Camera camera = new Camera();
        Vector2 cameraPosition = new Vector2(5, 5);
        Map map = new Map();
        int zoomLevel = 64;

        float pauseAlpha;
        InputAction pauseAction;
        ContentManager content;

        #endregion

        #region Initialization

        public TestTileEngineScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            pauseAction = new InputAction(
                new Buttons[] { Buttons.Start, Buttons.Back },
                new Keys[] { Keys.Escape },
                null,
                true);
        }

        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                content = MyGame.Singleton.Content;
                AssetsMapManager.Singleton.loadDecor(0, 0);
                MapData.load("mapXML/test.xml");
                map.generateMap(MapData.list[0]);
                MyGame.Singleton.ResetElapsedTime();
            }
        }


        public override void Deactivate()
        {
            base.Deactivate();
        }

        public override void Unload()
        {
        }

        #endregion

        #region Update and Draw

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (coveredByOtherScreen)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // Look up inputs for the active player profile.
            int playerIndex = (int)ControllingPlayer.Value;

            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];

            bool gamePadDisconnected = !gamePadState.IsConnected &&
                                       input.GamePadWasConnected[playerIndex];

            PlayerIndex player;
            if (pauseAction.Evaluate(input, ControllingPlayer, out player) || gamePadDisconnected)
            {
                MyGame.Singleton.AddScreen(new PauseMenuScreen(), ControllingPlayer);
            }
            else
            {
                // Otherwise move the player position.
                Vector2 movement = Vector2.Zero;

                if (keyboardState.IsKeyDown(Keys.Left))
                    movement.X -= 20f / zoomLevel;

                if (keyboardState.IsKeyDown(Keys.Right))
                    movement.X += 20f / zoomLevel;

                if (keyboardState.IsKeyDown(Keys.Up))
                    movement.Y -= 20f / zoomLevel;

                if (keyboardState.IsKeyDown(Keys.Down))
                    movement.Y += 20f / zoomLevel;

                if (keyboardState.IsKeyDown(Keys.O))
                {
                    zoomLevel += 2;
                    if (zoomLevel > 256)
                        zoomLevel = 256;
                }
                if (keyboardState.IsKeyDown(Keys.P))
                {
                    zoomLevel -= 2;
                    if (zoomLevel <= 0)
                        zoomLevel = 1;
                }

                Vector2 thumbstick = gamePadState.ThumbSticks.Left;

                movement.X += thumbstick.X;
                movement.Y -= thumbstick.Y;

                if (movement.Length() > 1)
                    movement.Normalize();

                cameraPosition += movement;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            // This game has a blue background.
            MyGame.Singleton.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.CornflowerBlue, 0, 0);


            SpriteBatch spriteBatch = MyGame.Singleton.SpriteBatch;

            camera.Position = cameraPosition;
            camera.ZoomLevel = zoomLevel;
            spriteBatch.Begin();
            map.drawBackground(gameTime, camera);
            map.drawForeground(gameTime, camera);
            spriteBatch.End();

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0 || pauseAlpha > 0)
            {
                float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);

                MyGame.Singleton.FadeBackBufferToBlack(alpha);
            }
        }


        #endregion
    }
}
